package com.hxy.platform.admin.controller;

import com.hxy.platform.admin.pojo.Employee;
import com.hxy.platform.admin.pojo.User;
import com.hxy.platform.admin.service.EmployeeService;
import com.hxy.platform.admin.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private EmployeeService employeeService;

    /**
     * 跳转到普通用户界面
     */
    @RequestMapping("page/user")
    public String userPage(ModelMap modelMap) {
        List<User> userList = userService.getAll();
        modelMap.addAttribute("userList", userList);
        return "user";
    }

    /**
     * 删除普通用户
     */
    @RequestMapping("user/deleteUser")
    public String deleteUser(@RequestParam("id") Integer id, ModelMap modelMap) {
        int result = userService.deleteById(id);
        if (result > 0) {
            List<User> userList = userService.getAll();
            modelMap.addAttribute("userList", userList);
            return "user";
        }
        return null;
    }

    /**
     * 跳转到管理员界面
     */
    @RequestMapping("page/admin")
    public String adminPage(ModelMap modelMap) {
        List<User> userList = userService.getAdminUser();
        modelMap.addAttribute("userList", userList);
        return "admin";
    }
    /**
     * 添加管理员
     */
    @RequestMapping("page/addAdmin")
    public String addAdminPage(User user, ModelMap modelMap) {
        if (org.apache.commons.lang3.StringUtils.isBlank(user.getEmail())) {
            return "addAdmin";
        }
        int result = userService.addAdminUser(user.getEmail(), user.getPassword());
        if (result > 0) {
            List<User> userList = userService.getAdminUser();
            modelMap.addAttribute("userList", userList);
            return "admin";
        }
        return null;
    }

    /**
     * 删除管理员
     */
    @RequestMapping("user/deleteAdminUser")
    public String deleteAdminUser(@RequestParam("id") Integer id, ModelMap modelMap) {
        int result = userService.deleteById(id);
        if (result > 0) {
            List<User> userList = userService.getAdminUser();
            modelMap.addAttribute("userList", userList);
            return "admin";
        }
        return null;
    }

    /**
     * 员工管理界面
     */
    @RequestMapping("page/employee")
    public String employeePage(ModelMap modelMap) {
        List<Employee> employeeList = employeeService.getAllEmployee();
        modelMap.addAttribute("employeeList", employeeList);
        return "employee";
    }

    /**
     * 添加员工
     */
    @RequestMapping("page/addEmployee")
    public String addEmployeePage(Employee employee, ModelMap modelMap) {
        if (StringUtils.isBlank(employee.getEmail())) {
            return "addEmployee";
        }
        int result = employeeService.addEmployee(employee);
        if (result > 0) {
            List<Employee> employeeList = employeeService.getAllEmployee();
            modelMap.addAttribute("employeeList", employeeList);
            return "employee";
        }
        return null;
    }

    /**
     * 删除员工
     */
    @RequestMapping("user/deleteEmployee")
    public String deleteEmployee(@RequestParam("id") Integer id, ModelMap modelMap) {
        int result = employeeService.deleteEmployeeById(id);
        if (result > 0) {
            List<Employee> employeeList = employeeService.getAllEmployee();
            modelMap.addAttribute("employeeList", employeeList);
                return "employee";
        }
        return null;
    }

}
